<template>
	<!-- <uni-popup ref="popup" type="bottom" @change="onChange"> -->
	<up-transition :show="show" mode="fade-up" duration="200">
		<view class="chat-at-box">
			<view class="chat-at-top">
				<!-- <view class="chat-at-tip">@</view> -->
				<view class="chat-at-tip">目标群聊</view>
				<up-icon name="close-circle" size="25" @click="show = !show"></up-icon>
				<!-- <button class="chat-at-btn" type="warn" size="mini" @click="onClean()">清空 </button>
				<button class="chat-at-btn" type="primary" size="mini" @click="onOk()">确定({{atUserIds.length}})
				</button> -->
			</view>
			<view class="search-bar">
				<up-search v-model="searchText" cancelButton="none" radius="100" placeholder="搜索" />
			</view>
			<view class="member-items">
				<scroll-view class="scroll-bar" scroll-with-animation="true" scroll-y="true">
					<view v-for="m in showGroups" v-show="m.showGroupName.includes(searchText)" :key="m.id">
						<view :class="['member-item',selectId==m.id && 'active']" @click="onSwitchChecked(m)">
							<head-image :name="m.showGroupName" :online="m.online" :url="m.headImage" :size="90"
								:isGroup="true"></head-image>
							<view class="member-name">{{ m.showGroupName}}</view>
							<!-- <view class="member-checked">
								<radio :checked="m.checked" @click.stop="onSwitchChecked(m)" />
							</view> -->
						</view>
					</view>
				</scroll-view>
			</view>
			<view class="sumbit">
				<button class="btncolor" @click="confirm">确定</button>
			</view>
		</view>
	</up-transition>
	<!-- </uni-popup> -->
</template>

<script>
	export default {
		name: "chat-at-box",
		props: {
			ownerId: {
				type: Number,
			},
			members: {
				type: Array
			},
			filterGroupId: {
				type: [Number, String],
				default: null
			}
		},
		data() {
			return {
				searchText: "",
				showMembers: [],
				showGroups: [],
				checkedGroup: null,
				show: false,
				selectId: null
			};
		},
		methods: {
			getData() {
				this.$http({
					url: "/group/findJoinedOfficialGroup",
					method: "GET"
				}).then((data) => {
					console.log("getData", data)
					this.showGroups = data
				})
			},
			init(atUserIds) {
				this.showMembers = [];
				let userId = this.userStore.userInfo.id;
				if (this.ownerId == userId) {
					this.showMembers.push({
						userId: -1,
						showNickName: "全体成员"
					})
				}
				this.members.forEach((m) => {
					if (!m.quit && m.userId != userId) {
						m.checked = atUserIds.indexOf(m.userId) >= 0;
						this.showMembers.push(m);
					}
				});
			},
			// 查询可以关联的群 合作推广页面使用
			getGroups(id) {
				this.$http({
					url: `/group/findBeRelatedGroup/${id}`,
					method: "GET"
				}).then((data) => {
					this.showGroups = data
				})
			},
			open() {
				this.show = true
				if (this.filterGroupId != null) {
					this.getGroups(this.filterGroupId)
				} else {
					this.getData()
				}

			},
			close() {
				this.show = false
			},
			confirm() {
				this.checkedGroup = this.showGroups.filter(item => item.checked)
				console.log(this.checkedGroup)
				if (this.checkedGroup.length > 0) {
					this.$emit("checked", this.checkedGroup)
					uni.showToast({
						title: '设置成功',
						icon: 'success',
						duration: 1000 // 设置显示时长为 3000 毫秒
					});
					this.close()
				} else {
					uni.showToast({
						title: "请选择目标群聊",
						icon: "none"
					})
				}
			},
			onSwitchChecked(member) {
				if (this.selectId == member.id) {
					this.selectId = null
					member.checked = false;
				} else {
					this.selectId = member.id
					this.showGroups.forEach(item => {
						item.checked = false
					})
					member.checked = true;
				}
				this.$emit("complete", this.atUserIds)
			},
			onClean() {
				this.showMembers.forEach((m) => {
					m.checked = false;
				})
			},
			onOk() {
				this.show = false
			},
			onChange(e) {
				if (!e.show) {
					this.$emit("complete", this.atUserIds)
				}
			}
		},
		computed: {
			atUserIds() {
				let ids = [];
				this.showMembers.forEach((m) => {
					if (m.checked) {
						ids.push(m.userId);
					}
				})
				return ids;
			}
		},
	}
</script>


<style lang="scss" scoped>
	.chat-at-box {
		position: relative;
		// border: #dddddd solid 1rpx;
		display: flex;
		flex-direction: column;
		background-color: white;
		padding: 10rpx;
		border-radius: 15rpx;
		z-index: 9999;

		.chat-at-top {
			display: flex;
			align-items: center;
			height: 70rpx;
			padding: 10rpx;

			.chat-at-tip {
				flex: 1;
				text-align: center;
			}

			.chat-at-btn {
				margin-left: 10rpx;
			}
		}


		.at-user-items {
			display: flex;
			align-items: center;
			height: 90rpx;

			.at-user-item {
				padding: 3rpx;
			}
		}



		.member-items {
			position: relative;
			flex: 1;
			overflow: hidden;

			.member-item.active {
				background-color: #f0f5fa;
			}

			.member-item {
				height: 120rpx;
				display: flex;
				position: relative;
				padding: 0 30rpx;
				align-items: center;
				background-color: white;
				white-space: nowrap;

				.member-name {
					flex: 1;
					padding-left: 20rpx;
					font-size: 30rpx;
					font-weight: 600;
					line-height: 60rpx;
					white-space: nowrap;
					overflow: hidden;
				}
			}

			.scroll-bar {
				height: 800rpx;
			}
		}

		.sumbit {
			padding: 0 30rpx;

			.btncolor {
				line-height: 75rpx;
				font-size: 30rpx;
				background-color: #048df5;
				color: #fff;
			}
		}


	}
</style>